
<?php $this->load->view('layout/lib_chart');?>

<?php 
$cats	=	array();
$values	=	array();
$current_year	=	date('Y');
$cats[]	=	$current_year;

foreach($endowments as $k => $v){
	$cats=doi_cho($cats,$v->maturity_year);
	
}

foreach($terms as $k =>$v){
	$year	=	$v->policy_incepted + $v->policy_term;
	$cats=doi_cho($cats,$year);
}

foreach($wls as $k =>$v){
	$year	=	$v->policy_incepted + $v->policy_term;
	$cats=doi_cho($cats,$year);
	if($v->step_up_booster==1){
		$cats=doi_cho($cats,$v->year_start);
		$cats=doi_cho($cats,$v->year_end);
	}
}

foreach($ilps as $k => $v){
	$year	=	$v->policy_incepted + $v->policy_term;
	$cats=doi_cho($cats,$year);
}
//term
foreach($cats as $k =>$v){
	
	if($k<count($cats)-1){
		$x_start	=	intval($cats[$k]);
		$x_end		=	intval($cats[$k+1]);
		$y_start	=	0;
		$y_end		=	0;
		$y_start_sadi	=	0;
		$y_end_sadi		=	0;
		$y_start_saci	=	0;
		$y_end_saci		=	0;
		
		foreach($endowments as $e){
			if($x_start < ($e->policy_incepted+$e->policy_term)){
				$y_start+=($e->sum_assured_death);
				$y_end+=($e->sum_assured_death);
				
				//sum_assured_disability
				$y_start_sadi+=($e->sum_assured_disability);
				$y_end_sadi+=($e->sum_assured_disability);
				//sum_assured_critical_illness
				$y_start_saci+=($e->sum_assured_critical_illness);
				$y_end_saci+=($e->sum_assured_critical_illness);
			}
		}
		
		//echo $y_end;
		
		foreach($terms as $t){
			if($x_start < ($t->policy_incepted+$t->policy_term)){
				$y_start+=($t->sum_assured_death	-	round((($t->sum_assured_death/$t->policy_term)*($x_start-$t->policy_incepted))));
				$y_end+=($t->sum_assured_death	-	round((($t->sum_assured_death/$t->policy_term)*($x_end-$t->policy_incepted))));
				
				//sum_assured_disability
				$y_start_sadi+=($t->sum_assured_disability	-	round((($t->sum_assured_disability/$t->policy_term)*($x_start-$t->policy_incepted))));
				$y_end_sadi+=($t->sum_assured_disability	-	round((($t->sum_assured_disability/$t->policy_term)*($x_end-$t->policy_incepted))));
				//sum_assured_critical_illness
				$y_start_saci+=($t->sum_assured_critical_illness	-	round((($t->sum_assured_critical_illness/$t->policy_term)*($x_start-$t->policy_incepted))));
				$y_end_saci+=($t->sum_assured_critical_illness	-	round((($t->sum_assured_critical_illness/$t->policy_term)*($x_end-$t->policy_incepted))));
			}
			
		}
		
		foreach($wls as $w){
			if($x_start < ($w->policy_incepted+$w->policy_term)){
				$y_start+=$w->sum_assured_death;
				$y_end+=$w->sum_assured_death;
				//sum_assured_disability
				$y_start_sadi+=($w->sum_assured_disability);
				$y_end_sadi+=($w->sum_assured_disability);
				//sum_assured_critical_illness
				$y_start_saci+=($w->sum_assured_critical_illness);
				$y_end_saci+=($w->sum_assured_critical_illness);
			}
			if($w->step_up_booster==1){
				if(intval($x_start)==intval($w->year_start)){
					$y_start+=$w->addictional_sum_assured_death;
					$y_end+=$w->addictional_sum_assured_death;
					//
					$y_start_sadi+=($w->addictional_sum_assured_disability);
					$y_end_sadi+=($w->addictional_sum_assured_disability);
					//sum_assured_critical_illness
					$y_start_saci+=($w->addictional_sum_assured_critical);
					$y_end_saci+=($w->addictional_sum_assured_critical);
				}
			}
		
		}
		
		foreach($ilps as $i){
			if($x_start < ($i->policy_incepted+$i->policy_term)){
				$y_start+=$i->sum_assured_death;
				$y_end+=$i->sum_assured_death;
				//sum_assured_disability
				$y_start_sadi+=($i->sum_assured_disability);
				$y_end_sadi+=($i->sum_assured_disability);
				//sum_assured_critical_illness
				$y_start_saci+=($i->sum_assured_critical_illness);
				$y_end_saci+=($i->sum_assured_critical_illness);
			}
		}
		
		$series[]=array(
			'data'	=>	array(
				array($x_start,$y_start),
				array($x_end,$y_end)
			),
			'marker' => array('symbol' => 'circle','radius' => 3)
		);
		
		//sadi
		$series_sadi[]=array(
			'data'	=>	array(
				array($x_start,$y_start_sadi),
				array($x_end,$y_end_sadi)
			),
			'marker' => array('symbol' => 'circle','radius' => 3)
		);
		//saci
		$series_saci[]=array(
			'data'	=>	array(
				array($x_start,$y_start_saci),
				array($x_end,$y_end_saci)
			),
			'marker' => array('symbol' => 'circle','radius' => 3)
		);
		
		//y plot line
		$y_plot_line[]=array(
			'value'	=>	$y_start,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
		);
		
		$y_plot_line[]=array(
			'value'	=>	$y_end,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
		);
		
		//sadi
		$y_plot_line_sadi[]=array(
			'value'	=>	$y_end_sadi,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
		);
		//saci
		$y_plot_line_saci[]=array(
			'value'	=>	$y_end_saci,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
		);
		//end y splot line
		
		//x splot line
		$x_plot_line[]=array(
			'value'	=>	$x_start,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
		);
		
		$x_plot_line[]=array(
			'value'	=>	$x_end,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
		);
		//end x splot line
	}
}
//
//$cats	=	json_encode($cats);
$series	=	json_encode($series);
$series_sadi	=	json_encode($series_sadi);
$series_saci	=	json_encode($series_saci);
$x_plot_line	=	json_encode($x_plot_line);
$y_plot_line	=	json_encode($y_plot_line);
$y_plot_line_sadi	=	json_encode($y_plot_line_sadi);
$y_plot_line_saci	=	json_encode($y_plot_line_saci);
$values	=	json_encode($values);

//sum retirement
$year_birday	=	date('Y',strtotime($_SESSION['customer']['date_of_birth']));
$year_retirement	=	$year_birday + ($paramter ? $paramter->retirement_age : 60);
$cats_re	=	array();

foreach($endowments as $k => $v){
	$year	=	$v->policy_incepted+$v->policy_term;
	$cats_re	=	doi_cho($cats_re,$year);
}

$cats_re	=	doi_cho($cats_re,$year_retirement);
foreach($wls as $k => $v){
	$year	=	$v->policy_incepted+$v->policy_term;
	$cats_re	=	doi_cho($cats_re,$year);
}

foreach($ilps as $k => $v){
	$year	=	$v->policy_incepted+$v->policy_term;
	$cats_re	=	doi_cho($cats_re,$year);
}

//$cats_re=doi_cho($cats_re,2050);
//$cats_re=doi_cho($cats_re,2055);

foreach($cats_re as $k => $v)
{
	if($k < count($cats_re)-1)
	{
		$x_re_start	=	$cats_re[$k];
		
		$x_re_end	=	$cats_re[$k+1];
		$x_re_center	=	round(($cats_re[$k+1]+$cats_re[$k])/2);
		$y_re_start	=	0;
		$y_re_center	=	0;
		$y_re_end	=	0;
		
		foreach($wls as $w)
		{
			$y_re_start+=$w->cva_retirement;
			$y_re_center+=$w->cva_retirement;
			$y_re_end+=$w->cva_retirement;
		}
		
		foreach($ilps as $i)
		{
			$y_re_start+=round($i->cva_retirement*(pow( (1 + ($i->expected_annual_return/100) ) ,(intval($x_re_start) - intval($year_retirement)))));
			$y_re_center+=round($i->cva_retirement*(pow( (1 + ($i->expected_annual_return/100) ) ,(intval($x_re_center) - intval($year_retirement)))));
			$y_re_end+=round($i->cva_retirement*(pow((1 + ($i->expected_annual_return/100) ),(intval($x_re_end) - intval($year_retirement)))));
			
		}
		
		foreach($endowments as $e)
		{
			if($x_re_start==$e->maturity_year)
			{
				$y_re_start+=$e->maturity_value;
				$y_re_center+=$e->maturity_value;
				$y_re_end+=$e->maturity_value;
			}
		}
	}	
	elseif($k==count($cats_re)-1)
	{
		$x_re_start	=	$cats_re[$k];
		$x_re_end	=	$x_re_start+2;
		$x_re_center	=	round(($x_re_end+$x_re_start)/2);
		foreach($wls as $w)
		{
			$y_re_start+=$w->cva_retirement;
			$y_re_center+=$w->cva_retirement;
			$y_re_end+=$w->cva_retirement;
		}
		
		foreach($ilps as $i)
		{
			$y_re_start = $y_re_end;
			$y_re_center=$y_re_start+round($i->cva_retirement*(pow((1 + ($i->expected_annual_return/100) ),(intval($x_re_center) - intval($year_retirement)))));
			$y_re_end=$y_re_start+round($i->cva_retirement*(pow((1 + ($i->expected_annual_return/100) ),(intval($x_re_end) - intval($year_retirement)))));
			
		}
	}
	
	$series_re[]=array(
		'data'	=>	array(
				array($x_re_start,$y_re_start),
				array($x_re_center,$y_re_center),
				array($x_re_end,$y_re_end)
			),
			'marker' => array('symbol' => 'circle','radius' => 2),
			'type'	=>	'spline'
	);
	
	//
	$y_re_splot_line[]=array(
		'value'	=>	$y_re_start,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
	);
	
	$y_re_splot_line[]=array(
		'value'	=>	$y_re_end,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
	);
	
	
	$x_re_splot_line[]=array(
		'value'	=>	$x_re_start,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
	);
	$x_re_splot_line[]=array(
		'value'	=>	$x_re_end,
			'dashStyle'	=> 'dash',
			'width'	=> 1,
			'color'	=>	'green',
			'label'	=>array(
				'align'	=>	'right',
				'y'	=>	12,
				'x'	=>	0
				)
	);
}

$series_re	=	json_encode($series_re);
$y_re_splot_line	=	json_encode($y_re_splot_line);
$x_re_splot_line	=	json_encode($x_re_splot_line);
/*
print_r($cats_re);
echo '<pre>';
print_r($series_re);
*/
?>
<script>
$(document).ready(function() {
	var chart = new Highcharts.Chart({
	    chart: {
	        renderTo: 'sum_sad',
	        defaultSeriesType: 'spline'
	    },
	    title:{
		    text: 'Sum of graph SAD'
		},
	    //colors:['#4572A7','#4572A7'],
	    xAxis:{
	    	plotLines: <?php echo $x_plot_line;?>,
	    	labels: {
				formatter: function() {
					return this.value + '';
				}
			},
			min:<?php echo $current_year?>,
			title:{ text: 'Year'}
		},
		yAxis:[{
			labels: {
				formatter: function() {
					return this.value + '$';
				}
			},
			title:{
				text: 'SAD'
			},
			plotLines: <?php echo $y_plot_line;?>,
			gridLineWidth : 0
		}],
		series:<?php echo $series;?>
	});

	//sadi
	var chart2 = new Highcharts.Chart({
	    chart: {
	        renderTo: 'sum_sadi',
	        defaultSeriesType: 'spline'
	    },
	    title:{
		    text: 'Sum of graph SADI'
		},
	    //colors:['#4572A7','#4572A7'],
	    xAxis:{
	    	plotLines: <?php echo $x_plot_line;?>,
	    	labels: {
				formatter: function() {
					return this.value + '';
				}
			},
			min: <?php echo $current_year?>,
			title:{ text: 'Year'}
		},
		yAxis:[{
			labels: {
				formatter: function() {
					return this.value + '$';
				}
			},
			title:{
				text: 'SADI'
			},
			plotLines: <?php echo $y_plot_line_sadi;?>,
			gridLineWidth : 0
		}],
		series:<?php echo $series_sadi;?>
	});
	//saci
	var chart3 = new Highcharts.Chart({
	    chart: {
	        renderTo: 'sum_saci',
	        defaultSeriesType: 'spline'
	    },
	    title:{
		    text: 'Sum of graph SACI'
		},
	    //colors:['#4572A7','#4572A7'],
	    xAxis:{
	    	plotLines: <?php echo $x_plot_line;?>,
	    	labels: {
				formatter: function() {
					return this.value + '';
				}
			},
			min: <?php echo $current_year;?>,
			title:{ text: 'Year'}
		},
		yAxis:[{
			labels: {
				formatter: function() {
					return this.value + '$';
				}
			},
			title:{
				text: 'SACI'
			},
			plotLines: <?php echo $y_plot_line_saci;?>,
			gridLineWidth : 0
		}],
		series:<?php echo $series_saci;?>
	});

	//retirement
	var chart4 = new Highcharts.Chart({
	    chart: {
	        renderTo: 'sum_retirement',
	        defaultSeriesType: 'spline'
	    },
	    title:{
		    text: 'Sum of graph Retirement'
		},
	    colors:['#4572A7','#4572A7'],
	    xAxis:{
	    	plotLines: <?php echo $x_re_splot_line;?>,
	    	labels: {
				formatter: function() {
					return this.value + '';
				}
			},
			min:<?php echo $current_year;?>,
			title:{ text: 'Year'}
		},
		yAxis:[{
			labels: {
				formatter: function() {
					return this.value + '$';
				}
			},
			title:{
				text: 'Retirement'
			},
			plotLines: <?php echo $y_re_splot_line;?>,
			gridLineWidth : 0,
			showLastLabel: true,
			maxPadding: 0.05
		}],
		series:<?php echo $series_re;?>
	});
});
</script>

	<div></div>
		<table>
			<tr>
				<td><div id="sum_sad" style="width: 400px; height: 400px; margin: 0 auto"></div></td>
				<td><div id="sum_sadi" style="width: 400px; height: 400px; margin: 0 auto"></div></td>
			</tr>
			<tr>
				<td style="vertical-align: bottom;"><div id="sum_saci" style="width: 400px; height: 400px; margin: 0 auto"></div></td>
				<td style="vertical-align: bottom;"><div id="sum_retirement" style="width: 400px; height: 600px; margin: 0 auto"></div></td>
			</tr>
		</table>